#ifndef PHP_KEYLISTNER_HPP
#define PHP_KEYLISTNER_HPP

#include "guichan/keylistener.hpp"
#include "guichan/platform.hpp"

#include "guichan/key.hpp"

#include "php_guichan.h"

#include <string>

namespace php
{
    /**
     * An implementation of a text field where a user can enter a line of text.
     */
    class GCN_CORE_DECLSPEC PhpKeyListner:
        public gcn::KeyListener
    {
    public:
        /**
         * Constructor.
         */
        PhpKeyListner(void *phpcallback) {
            php_callback *t;
            t=(php_callback *)phpcallback;
        	call=t->callback;
        	params=t->params;
        	
        	MAKE_STD_ZVAL(retval);
        }
        
        void keyPressed(gcn::KeyEvent& keyEvent)
        {
            zval *obj,*fun;
	        Uint32 returns;
	        
	        TSRMLS_FETCH();
	        
            gcn::Key key = keyEvent.getKey();
    
            if (key.getValue() != gcn::Key::F5)
            {
                //keyEvent.consume();
                
                switch(Z_TYPE_P(call)) {
            	case IS_ARRAY:
            		getArrayValueByIndex(*call,0,&obj);
            		getArrayValueByIndex(*call,1,&fun);
            		if (call_user_function(EG(function_table),&obj,fun,retval,1,&params TSRMLS_CC)) {
            			php_error(E_WARNING, "php_callback: callback failed");
            		}
            		break;
            	case IS_STRING:
            		if (call_user_function(EG(function_table),NULL,call,retval,1,&params TSRMLS_CC)) {
            			php_error(E_WARNING, "php_callback: callback failed");
            		}
            		break;
            	default:
            		php_error(E_WARNING, "php_callback: callback not accepted");
            		break;
            	}
            	//convert_to_long(retval);
            	//returns=Z_LVAL_P(retval);
            	//return returns;
            }
        }
        
        //void keyReleased(KeyEvent& keyEvent);

    protected:
    	zval *call;
    	zval *params;
    	zval *retval;
    };
}

#endif // end PHP_KEYLISTNER_HPP
